//index.js
//获取应用实例
const app = getApp()
let timeStamp = "";
let packageT = "";
let paySign = "";
let appId = "";
let signType = "MD5";
let nonceStr = "";
var name = "1";

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    console.log(  options);
    timeStamp = options.timeStamp;
    packageT = options.package;
    paySign = options.paySign;
    appId = options.appId;
    nonceStr = options.nonceStr;
    name = options.name;
    wx.showToast({
      title: "APP:" + options.appId,
    })
    // 页面创建时执行    
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    alert(0);
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  
  test:function(){
    alert(name);
    wx.request({
      url: 'https://lyupg.utools.club/UPG/pay/payBService.c?do=service', //接口地址
      data: {  },
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        console.log("返回结果",res.data)
      }
    }) 
  },
  login: function () {
    wx.login({
      success: function (res) {
        console.log(res.code)
        //发送请求
        wx.request({
          url: 'https://lyupg.utools.club/UPG/test/service/getOpenId', //接口地址 
          data: { code: res.code },
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res.data)
          }
        })
      }
    })
  },
  pay: function (e) {
     
        //客户单发起微信请求
        wx.requestPayment(
          { 
            "timeStamp": timeStamp,
            "package": packageT,
            "paySign": paySign,
            "appId": appId,
            "signType": "MD5",
            "nonceStr": nonceStr,
            'success': function (res) {
              wx.showToast({
                title: '支付成功',
                duration: 1200
              })
              console.log('支付成功');
              setTimeout(function () {
                wx.redirectTo({
                  url: '../pay/payok?paymoney=' + that.data.paymoney + '&body=' + that.data.contents
                })
              }, 1000);
              //这里完成跳转
            },
            'fail': function (res) {
              if (res.errMsg == "requestPayment:fail cancel") {
                wx.showToast({
                  title: '支付取消',
                  duration: 1200
                })
              } else {
                wx.showToast({
                  title: '支付失败',
                  image: '../../images/error.png',
                  duration: 1200
                })
              }
              //支付失败
              console.log(res);
            },
            'complete': function (res) { }
          })
  }, 
})
